<?php
// no direct access
defined( '_VALID_MOS' ) or die( 'Restricted access' );

$nb_contents = intval($params->get( 'nb_contents', 3 )) ;
$image = intval($params->get( 'image', 1 )) ;
$sort_sql = intval($params->get( 'random',0));
$directory = intval($params->get( 'directory', mosGetParam( $_GET, 'directory', 1 ) )) ;
$catselect = $params->get('catselect',"no");
$displaycategory = intval($params->get( 'displaycategory',1));
$displaydate = intval($params->get( 'displaydate',1));

require_once(JPATH_BASE.'/components/com_boss/boss.tools.php');

switch($sort_sql)
{
	/* Popular */
	case 2:
		$order_sql = "ORDER BY a.views DESC,a.date_created DESC ,a.id DESC ";
		break;

	/* Random */
	case 1:
		$order_sql = "ORDER BY RAND() ";
		break;

	/* Latest */
	case 0:
	default:
		$order_sql = "ORDER BY a.date_created DESC ,a.id DESC ";
		break;
}

$cat_query = "";
switch($catselect)
{
	case "no";
		break;

	case "-1":
		$catid = intval(mosGetParam( $_GET, 'catid', 0 ));
		if (($catid != 0)&&($catid != -1))
		{
			$cat_query = "c.id = $catid AND ";
		}
		break;
	default:
		$cat_query = "c.id = $catselect AND ";
		break;
}

$q = "SELECT a.id, a.content_headline, c.id as category, a.date_created, p.id as parentid,".
     "\n p.name as parent,c.id as catid, c.name as cat".
	 "\n FROM #__boss_".$directory."_contents as a ".
     "\n LEFT JOIN #__boss_".$directory."_content_category_href as cx ON a.id = cx.content_id".
	 "\n LEFT JOIN #__boss_".$directory."_categories as c ON cx.content_id = c.id".
	 "\n LEFT JOIN #__boss_".$directory."_categories as p ON c.parent = p.id".
	 "\n WHERE $cat_query c.published = 1 and a.published = 1 $order_sql LIMIT 0, $nb_contents";
$database->setQuery($q);

$contents = $database->loadObjectList();

// get configuration
$database->setQuery( "SELECT * FROM #__boss_config WHERE id = $directory");
$confs = $database->loadObjectList();
if ($database -> getErrorNum()) {
	echo $database -> stderr();
	return false;
}

$template_name = $confs[0]->template;

if (!defined( '_MOS_BOSS_MODULE' )) {
	/** ensure that functions are declared only once */
	define( '_MOS_BOSS_MODULE', 1 );
	
	function moduleReorderDate( $date ){
		return mosFormatDate($date);
	}
	
	function displayVerticalLatestContents($contents,$image,$nb_images,$displaycategory,$displaydate,$directory,$template_name){
		
		if ($image == 1)
		{
	    ?>
			<ul class="jdirectory_ver_list">
			<?php
			if(isset($contents[0])) {
			foreach($contents as $row) {
			?>
				<li>
				<?php
                $itemid = getBossItemid($directory, $row->category);
				$linkTarget = sefRelToAbs("index.php?option=com_boss&amp;directory=$directory&amp;task=show_content&amp;contentid=".$row->id."&amp;Itemid=".$itemid);
				$ok = 0;$i=1;
				while(!$ok)
				{   
					if ($i < $nb_images + 1)
					{
						$ext_name = chr(ord('a')+$i-1);
						$pic = JPATH_BASE."/images/boss/$directory/contents/".$row->id.$ext_name."_t.jpg";
						if (file_exists( $pic)) 
						{
							echo "<div align='center'><a href='".$linkTarget."'><img src='".JPATH_SITE."/images/com_jdirectory/$directory/contents/".$row->id.$ext_name."_t.jpg' alt='".htmlspecialchars(stripslashes($row->content_headline),ENT_QUOTES)."' border='0' /></a>";
							$ok = 1;
						}
					}
					else
					{
						echo "<div align='center'><a href='".$linkTarget."'><img src='".JPATH_SITE."/templates/com_boss/$template_name/images/nopic.gif' alt='noimage' border='0' /></a>"; 
						$ok = 1;
					}   
					$i++;   	
				}
				echo "<br /><a href='".$linkTarget."'>".stripslashes($row->content_headline)."</a>";
				if ($displaycategory == 1)
					echo "<br /><span class=\"jdirectory_cat\">(".$row->parent." / ".$row->cat.")</span>";
				if ($displaydate == 1)
					echo "<br />".moduleReorderDate($row->date_created);
				echo "</div>";
				?>
				</li>
		<?php
			} }
			?>
			</ul>
			<?php
		}
		else
		{
			?>
			<ul class="mostread">
			<?php
			if (isset($contents[0])){
			foreach($contents as $row) {
			?>
				<li class="mostread">
				<?php
                    $itemid = getBossItemid($directory, $row->category);
					$linkTarget = sefRelToAbs("index.php?option=com_boss&amp;directory=$directory&amp;task=show_content&amp;contentid=".$row->id."&amp;catid=".$row->category."&amp;Itemid=".$itemid);
					echo "<a href='$linkTarget'>".stripslashes($row->content_headline);
					if ($displaycategory == 1)
						echo "&nbsp;<span class='jdirectory_cat'>(".$row->parent." / ".$row->cat.")";
					if ($displaydate == 1)
						echo "&nbsp;".moduleReorderDate($row->date_created)."</span></a>"; 
					else
						echo "</span></a>";
				?>
				</li>
		<?php
			}}
			?>
			</ul>
			<?php
		}	
	}
	
	function displayHorizontalLatestContents($contents,$image,$nb_images,$displaycategory,$displaydate,$directory,$template_name){
		if ($image == 1){
		?>
			<div class='jdirectory_box_module_2'>
			<table class='jdirectory_inner_box_2' width="100%">
			<tr align="center">
			<?php
			$contents_by_row = 3;
			$num_contents = 0;
			if (isset($contents[0])) {
			foreach($contents as $row) {
				if ($num_contents >= $contents_by_row) {
					echo "</tr><tr>";
					$num_contents = 0;
				}
				?>
				<td>
				<?php
                $itemid = getBossItemid($directory, $row->category);
				$linkTarget = sefRelToAbs("index.php?option=com_boss&amp;directory=$directory&amp;task=show_content&amp;contentid=".$row->id."&amp;Itemid=".$itemid);
				$ok = 0;$i=1;
				while(!$ok)
				{
					if ($i < $nb_images + 1)
					{
						$ext_name = chr(ord('a')+$i-1);
						$pic = JPATH_BASE."/images/boss/$directory/contents/".$row->id.$ext_name."_t.jpg";
						if (file_exists( $pic)) 
						{
							echo "<div align='center'><a href='".$linkTarget."'><img src='".JPATH_SITE."/images/boss/$directory/contents/".$row->id.$ext_name."_t.jpg' alt='".htmlspecialchars(stripslashes($row->content_headline),ENT_QUOTES)."' border='0' /></a>";
							$ok = 1;
						}
					}
					else
					{
						echo "<div align='center'><a href='".$linkTarget."'><img src='".JPATH_SITE."/templates/com_boss/$template_name/images/nopic.gif' alt='noimage' border='0' /></a>";
						$ok = 1;
					}   
					$i++;   	
				}
					
				echo "<br /><a href='$linkTarget'>".stripslashes($row->content_headline)."</a>"; 
				if ($displaycategory == 1)
					echo "<br /><span class=\"jdirectory_cat\">(".$row->parent." / ".$row->cat.")</span>";
				if ($displaydate == 1)
					echo "<br />".moduleReorderDate($row->date_created);
				echo "</div>";
				?>
				</td>
			<?php
				$num_contents ++;
			} }
			for(;$num_contents < $contents_by_row;$num_contents++)
			{
				echo "<td></td>";
			}
			?>
			</tr>
			</table>
			</div>
		<?php
		}
		else{
			?>
			<ul class="mostread">
			<?php
			if (isset($contents[0])) {
			foreach($contents as $row) {
			?>
				<li class="mostread">
				<?php
                    $itemid = getBossItemid($directory, $row->category);
					$linkTarget = sefRelToAbs("index.php?option=com_boss&amp;directory=$directory&amp;task=show_content&amp;contentid=".$row->id."&amp;catid=".$row->category."&amp;Itemid=".$itemid);
					echo "<a href='$linkTarget'>".stripslashes($row->content_headline)."</a>"; 
					if ($displaycategory == 1)
						echo "<br /><span class=\"jdirectory_cat\">(".$row->parent." / ".$row->cat.")</span>";
					if ($displaydate == 1)
						echo "<br />".moduleReorderDate($row->date_created);
				?>
				</li>
		<?php
			} }
			?>
			</ul>
			<?php
		}
	}
    $mainframe->addCSS(JPATH_SITE.'/templates/com_boss/'.$template_name.'/css/boss.css');
}




switch ( $params->get( 'style', 'hor' ) ) {
	case 'ver':
		displayVerticalLatestContents($contents,$image,$confs[0]->nb_images,$displaycategory,$displaydate,$directory,$template_name);
		break;
	
	case 'hor':
	default:
		displayHorizontalLatestContents($contents,$image,$confs[0]->nb_images,$displaycategory,$displaydate,$directory,$template_name);
		break;
}
?>